package com.ruoyi.ring.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.ring.domain.RingPhysicalConditionLayout;
import com.ruoyi.ring.mapper.RingPhysicalConditionLayoutMapper;
import com.ruoyi.ring.service.IRingPhysicalConditionLayoutService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 身体状况布局Service业务层处理
 * 
 * @author liuwp
 * @date 2025-10-09
 */
@Slf4j
@Service
public class RingPhysicalConditionLayoutServiceImpl implements IRingPhysicalConditionLayoutService 
{
    @Autowired
    private RingPhysicalConditionLayoutMapper ringPhysicalConditionLayoutMapper;

    /**
     * 查询身体状况布局
     * 
     * @param id 身体状况布局主键
     * @return 身体状况布局
     */
    @Override
    public RingPhysicalConditionLayout selectRingPhysicalConditionLayoutById(Long id)
    {
        return ringPhysicalConditionLayoutMapper.selectRingPhysicalConditionLayoutById(id);
    }

    /**
     * 查询身体状况布局列表
     * 
     * @param ringPhysicalConditionLayout 身体状况布局
     * @return 身体状况布局
     */
    @Override
    public List<RingPhysicalConditionLayout> selectRingPhysicalConditionLayoutList(RingPhysicalConditionLayout ringPhysicalConditionLayout)
    {
        return ringPhysicalConditionLayoutMapper.selectRingPhysicalConditionLayoutList(ringPhysicalConditionLayout);
    }

    /**
     * 根据业务主键查询一条记录：身体状况布局
     *
     * @param ringPhysicalConditionLayout 身体状况布局
     * @return 身体状况布局
     */
    @Override
    public RingPhysicalConditionLayout selectOneRingPhysicalConditionLayout(RingPhysicalConditionLayout ringPhysicalConditionLayout)
    {
        return ringPhysicalConditionLayoutMapper.selectOneRingPhysicalConditionLayout(ringPhysicalConditionLayout);
    }

    /**
     * 新增身体状况布局
     * 
     * @param ringPhysicalConditionLayout 身体状况布局
     * @return 结果
     */
    @Override
    public int insertRingPhysicalConditionLayout(RingPhysicalConditionLayout ringPhysicalConditionLayout)
    {
        ringPhysicalConditionLayout.setCreateTime(DateUtils.getNowDate());
        return ringPhysicalConditionLayoutMapper.insertRingPhysicalConditionLayout(ringPhysicalConditionLayout);
    }

    /**
     * 修改身体状况布局
     * 
     * @param ringPhysicalConditionLayout 身体状况布局
     * @return 结果
     */
    @Override
    public int updateRingPhysicalConditionLayout(RingPhysicalConditionLayout ringPhysicalConditionLayout)
    {
        ringPhysicalConditionLayout.setUpdateTime(DateUtils.getNowDate());
        return ringPhysicalConditionLayoutMapper.updateRingPhysicalConditionLayout(ringPhysicalConditionLayout);
    }

    @Override
    public int updateRingPhysicalConditionLayoutCondition(RingPhysicalConditionLayout ringPhysicalConditionLayout, RingPhysicalConditionLayout ringPhysicalConditionLayoutCondition)
    {
                ringPhysicalConditionLayout.setUpdateTime(DateUtils.getNowDate());
        return ringPhysicalConditionLayoutMapper.updateRingPhysicalConditionLayoutCondition(ringPhysicalConditionLayout, ringPhysicalConditionLayoutCondition);
    }

    /**
     * 批量删除身体状况布局
     * 
     * @param ids 需要删除的身体状况布局主键
     * @return 结果
     */
    @Override
    public int deleteRingPhysicalConditionLayoutByIds(Long[] ids)
    {
        return ringPhysicalConditionLayoutMapper.deleteRingPhysicalConditionLayoutByIds(ids);
    }

    /**
     * 删除身体状况布局信息
     * 
     * @param id 身体状况布局主键
     * @return 结果
     */
    @Override
    public int deleteRingPhysicalConditionLayoutById(Long id)
    {
        return ringPhysicalConditionLayoutMapper.deleteRingPhysicalConditionLayoutById(id);
    }

    /**
     * 条件删除身体状况布局
     *
     * @param ringPhysicalConditionLayout 身体状况布局
     * @return 结果
     */
    @Override
    public int deleteRingPhysicalConditionLayoutByCondition(RingPhysicalConditionLayout ringPhysicalConditionLayout)
    {
        return ringPhysicalConditionLayoutMapper.deleteRingPhysicalConditionLayoutByCondition(ringPhysicalConditionLayout);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int editLayout(List<RingPhysicalConditionLayout> list) {
        try {
            Date date = new Date();
            long userId = SecurityUtils.getUserId();
            ringPhysicalConditionLayoutMapper.deleteByUserId(userId);
            int result = 0;
            for (RingPhysicalConditionLayout layout : list){
                layout.setUserId(userId);
                layout.setCreateTime(date);
                result = ringPhysicalConditionLayoutMapper.insertRingPhysicalConditionLayout(layout);
            }
            return result;
        }catch (Exception e){
            log.error(e.getMessage());
            throw new RuntimeException("修改布局失败");
        }

    }

}
